package com.wd.mqtt;


import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;
import io.vertx.mqtt.messages.MqttConnAckMessage;

public class VertxmqttImpl implements MqttClientApi {
    private String address;

    private MqttClient mqttClient;

    public VertxmqttImpl(String address) {
        this.address = address;
    }

    @Override
    public void conect(String clientId) {
        String[] strs =  address.split(":");
        String ip = strs[1].substring(2);
        int port = Integer.parseInt(strs[2]);

        MqttClientOptions options = new MqttClientOptions().setKeepAliveInterval(2);
        options.setClientId(clientId);
        options.setReceiveBufferSize(1024*1);
        options.setMaxMessageSize(1024*1);

        VertxOptions vertxOptions = new VertxOptions();

        Vertx vertx = Vertx.vertx(vertxOptions);

        MqttClient mqttClient = MqttClient.create(vertx);


        mqttClient.connect(port, ip, new Handler<AsyncResult<MqttConnAckMessage>>() {
            @Override
            public void handle(AsyncResult<MqttConnAckMessage> event) {
                event.result();
            }
        });
    }

    @Override
    public void push() {

    }
}
